var _bp = location.pathname.substring(0, location.pathname.indexOf("/", 1));
var _bp = getRootPath();
/*参数说明：
targetId，存储文件ID的文本域(隐藏域)的HTML代码中的id，字符串，必选项
targetName，存储文件名的文本域(隐藏域)的HTML代码中的id，字符串，可选项，为空时不回写文件名
targetUrl，存储文件完整URL路径的文本域(HTML对象)的HTML代码中的id，字符串，可选项，为空时不回写完整URL路径
fileIds,已上传的文件ID，字符串，以,分隔，可选项，为空时默认使用targetId所代表的HTML元素中的value值或innerText值
option，打开窗口的选项，对象，jSon串，具体参考artDialog的参数，默认为
{title:'请上传文件',lock:true,width:600,height:400,left:center,top:center}
*/
//document.write('<script type="text/javascript" src="'+_bp+'/common/js/jsdebug/logger.js"></script>');

function fileupload(targetId,targetName,targetUrl,fileType,option,callback){
	if(!targetId){
		alert('没有指定回写文件ID的域ID');
		return;
	}
	if(!targetName)targetName='';
	if(!targetUrl)targetUrl='';
	if(!fileType)fileType='';
	var fileIds = '';
	try{fileIds = _$(targetId).value||_$(targetId).innerText}
	catch(e){fileIds='';}
	if(!option){
		option = {};
	}
	var o = {title:'\u8bf7\u9009\u62e9\u4e0a\u4f20\u6587\u4ef6',shadow:false,width:600,height:450};
	for(var p in o){
		if(option[p]==undefined)option[p]=o[p];
	}
	option.iframe = _bp+'/pub/upload/PubUpload.action?targetId='+
		targetId+'&targetName='+targetName+'&targetUrl='+targetUrl+
		'&type='+fileType+'&fileIds='+fileIds;
	if(option.param){
		for(var p in option.param){
			option.iframe += '&'+p+'='+option.param[p];
		}
	}
	option.html = '<iframe frameborder="0" style="width:100%;height:100%;" src="'+option.iframe+'"></iframe>'
	try{
		getUploadWindow().window(option);
		//清除窗口内容，避免打开窗口时显示前一操作时的页面信息内容
		getUploadWindow().empty();
		//加载添加页面
		getUploadWindow().html('<iframe src="'+option.iframe+'" frameborder="0" style="width:100%;height:100%;"></iframe>');
		//打开添加窗口
		getUploadWindow().window('open');
		getUploadWindow().data('callbackFun',callback);
	}catch(e){//当弹出层不能正常工作时，使用弹出窗口
		//alert(e);
		window.open(option.iframe,'fileupload','"resizable=yes, width=700, height=500, top=50, toolbar=no, menubar=no,scrollbars=no,location=no,status=no');
	}
}

function getUploadWindow(){
	if($('#uploadWindow').length==0){
		var div = $('<div></div>');
		div.attr({id:'uploadWindow',className:'easyui-window',closed:"true",modal:"true",collapsible:"false",minimizable:"false",maximizable:"false",closable:"true"});
		div.appendTo(document.body);
	}
	return $('#uploadWindow');
}

function hideUploadWindow(){
	var dia = getUploadWindow();
	if(dia.data('callbackFun')){
		var fun = dia.data('callbackFun');
		if(typeof(fun)=='function'){
			try{fun();}catch(e){;}
		}
		else{
			try{eval(fun);}catch(e){;}
		}
	}
	dia.window('close');
}

String.prototype.trim=function () {return this.replace(/(^\s+)|(\s+$)/g,"");}
if(!window._$)window._$=function(id){return document.getElementById(id)}